package com.echo.fragment;

import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.activeandroid.query.Select;
import com.echo.activity.R;
import com.echo.adapter.HistoryChartsAdapter;
import com.echo.model.DayData;
import com.echo.util.DateUtil;
import com.github.mikephil.charting.components.YAxis;
import com.github.mikephil.charting.data.Entry;
import com.github.mikephil.charting.data.LineData;
import com.github.mikephil.charting.data.LineDataSet;
import com.github.mikephil.charting.interfaces.datasets.ILineDataSet;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import in.srain.cube.views.ptr.PtrDefaultHandler;
import in.srain.cube.views.ptr.PtrFrameLayout;
import in.srain.cube.views.ptr.PtrHandler;

/**
 * Created by EchoGe on 2016/6/12.
 */
public class HistoryChartsMonthFragment extends HistoryChartsFragment{
    private View mView;

    private RecyclerView mChartsRecycler;
    private HistoryChartsAdapter mAdapter;

    private List<DayData> mDayData=new ArrayList<DayData>();
    private List<LineData> mLineData=new ArrayList<LineData>();

    private String[] mTitles;

    private int[] mMaxVals=new int[3];

    private PtrFrameLayout mPtrFrame;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        if (mView == null) {
            mView = inflater.inflate(R.layout.fragment_charts, container, false);
            initView(mView);
            loadData();
        }

        return mView;
    }

    private void initView(View mView) {
        mTitles=getResources().getStringArray(R.array.chart_items);
        mChartsRecycler= (RecyclerView) mView.findViewById(R.id.charts_recycler);
        mChartsRecycler.setLayoutManager(new LinearLayoutManager(getActivity(), LinearLayoutManager.VERTICAL,false));
        mPtrFrame= (PtrFrameLayout) mView.findViewById(R.id.history_ptr_frame);
        mPtrFrame.setPtrHandler(new PtrHandler() {
            @Override
            public boolean checkCanDoRefresh(PtrFrameLayout frame, View content, View header) {
                return PtrDefaultHandler.checkContentCanBePulledDown(frame, content, header);
            }

            @Override
            public void onRefreshBegin(PtrFrameLayout frame) {
                //int position=mViewPager.getCurrentItem();
                frame.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        loadData();
                        //mAdapter.refresh();
                        mPtrFrame.refreshComplete();
                    }
                },1800);
            }
        });
    }

    public void loadData(){
        //DayData dayData1=new Select().from(DayData.class).where("date=?",DateUtil.getDateStr(Calendar.getInstance())).orderBy("RANDOM()").executeSingle();
        Calendar[] week= DateUtil.getPreMonthCalendar();

        mDayData=new ArrayList<DayData>();
        mLineData=new ArrayList<LineData>();
        List xVals=new ArrayList();
        List yStepVals=new ArrayList();
        List yTimeVals=new ArrayList();
        List yDistanceVals=new ArrayList();
        for(int i=0;i<week.length;i++){
            xVals.add(week[i].getTime().getDate()+"");
            DayData dayData=new Select().from(DayData.class).where("date=?",DateUtil.getDateStr(week[i])).executeSingle();
            if(dayData!=null)
                mDayData.add(dayData);
            else{
                dayData=new DayData();
                dayData.setSteps(0);
                dayData.setDate(DateUtil.getDateStr(week[i]));
                dayData.setTime(0);
                dayData.setDistance(0);
                mDayData.add(dayData);
            }
            if(dayData.getSteps()>mMaxVals[0])
                mMaxVals[0]=dayData.getSteps();
            if(dayData.getTime()>mMaxVals[1])
                mMaxVals[1]=dayData.getTime();
            if(dayData.getDistance()>mMaxVals[2])
                mMaxVals[2]=dayData.getDistance();
            yStepVals.add(new Entry(dayData.getSteps(),i));
            yTimeVals.add(new Entry(dayData.getTime(),i));
            yDistanceVals.add(new Entry(dayData.getDistance(),i));
        }

        mLineData.add(getLineData(xVals,yStepVals,"步数"));
        mLineData.add(getLineData(xVals,yTimeVals,"运动时间"));
        mLineData.add(getLineData(xVals,yDistanceVals,"运动距离"));
        mAdapter=new HistoryChartsAdapter(getActivity(),mTitles,mLineData,mMaxVals);
        mChartsRecycler.setAdapter(mAdapter);

    }

    private LineData getLineData(List xVals,List yVals,String title){
        LineDataSet set1;
        set1 = new LineDataSet(yVals, title);

        set1.setAxisDependency(YAxis.AxisDependency.LEFT);
        set1.setColor(Color.RED);
        set1.setCircleColor(Color.RED);
        set1.setLineWidth(2.5f);
        set1.setCircleRadius(4.5f);
        //set1.setFillAlpha(65);
        //set1.setFillColor(Color.RED);
        set1.setHighLightColor(Color.rgb(244, 117, 117));
        set1.setDrawCircleHole(true);

        ArrayList<ILineDataSet> dataSets = new ArrayList<ILineDataSet>();
        dataSets.add(set1);

        // create a data object with the datasets
        LineData data = new LineData(xVals, dataSets);
        data.setValueTextColor(Color.TRANSPARENT);
        data.setValueTextSize(9f);

        return data;
    }
}
